package cn.jiangjiesheng.bootstrap.database;

import cn.jiangjiesheng.bootstrap.database.properties.RedisProperties;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.integration.redis.util.RedisLockRegistry;

import java.util.List;
import java.util.Map;

/**
 * Created by dev@jiangjiesheng.cn on 2021/9/3  <br/>
 *
 * @author dev@jiangjiesheng.cn
 */
@EnableConfigurationProperties(RedisProperties.class)
@Configuration
@ConditionalOnMissingBean(name = "redissonClient")
@RefreshScope
@EnableCaching
@Slf4j
public class RedisAutoConfiguration {

    @Bean
    public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) {
        long defaultExpireTime = 10000L;
        return new RedisLockRegistry(redisConnectionFactory, "redis-lock", defaultExpireTime);
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean(LettuceConnectionFactory.class)
    public LettuceConnectionFactory lettuceConnectionFactory(RedisProperties redisProperties) {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort());
        configuration.setDatabase(redisProperties.getDatabase());
        configuration.setPassword(redisProperties.getPassword());
        return new LettuceConnectionFactory(configuration);
    }

    @Bean
    @ConditionalOnMissingBean(RedisTemplate.class)
    public RedisTemplate<String, List<String>> stringListRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, List<String>> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        // Replace the default JdkSerializationRedisSerializer with Jackson2JsonRedisSerializer to serialize and deserialize the Redis value.
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
                ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(mapper);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // String serialization of keys
        template.setKeySerializer(stringRedisSerializer);
        // String serialization of hash keys
        template.setHashKeySerializer(stringRedisSerializer);
        // Jackson serialization of values
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // Jackson serialization of hash values
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(RedisTemplate.class)
    public RedisTemplate<String, Map<String, List<String>>> intMappedStringListRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Map<String, List<String>>> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        // Replace the default JdkSerializationRedisSerializer with Jackson2JsonRedisSerializer to serialize and deserialize the Redis value.
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
                ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(mapper);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // String serialization of keys
        template.setKeySerializer(stringRedisSerializer);
        // String serialization of hash keys
        template.setHashKeySerializer(stringRedisSerializer);
        // Jackson serialization of values
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // Jackson serialization of hash values
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}
